export const state = () => ({
    signIn: true,
    account: "test"
})

export const mutations = {
    setSignIn(state, signIn) {
        state.signIn = signIn
    },
    setAccount(state, account) {
        state.account = account
    }
}

export const actions = {
    getAccount({}, { type, id }) {
        return this.$api.account.getAccount(type, id).then((res) => {
            if(res.data.status == 'OK') {
                return Promise.resolve(res.data.data)
            }
            else {
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    getAccountList({},{type,username,password}) {
        return this.$api.account.getAccountList(type,username,password).then((res) => {
            if(res.data.status=="OK"){
                return Promise.resolve(res.data.data)
            }
            else{
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        },(error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error)=>{
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    resetAccount({},{type,username,password}){
        return this.$api.account.resetAccount(type,username,password).then((res)=>{
            if(res.data.status=="OK"){
                return Promise.resolve(res.data.msg)
            }
            else{
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        },(error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error)=>{
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    updateAccount({},{type,username,email,firstName,lastName,status,address1,address2,city,state,zip,country,phone}){
        return this.$api.account.updateAccount(type,username,email,firstName,lastName,status,address1,address2,city,state,zip,country,phone).then((res)=>{
            if(res.data.status=="OK"){
                return Promise.resolve(res.data.msg)
            }
            else{
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        },(error) => {
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error)=>{
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    deleteAccount({},{username}){
        return this.$api.account.deleteAccount(username).then((res)=>{
            if(res.data.status=="OK"){
                return Promise.resolve(res.data.msg)
            }
            else
            {
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        },(error)=>{
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error)=>{
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    addAccount({},{username,email,firstName,lastName,status,address1,address2,city,state,zip,country,phone}){
        return this.$api.account.addAccount({username,email,firstName,lastName,status,address1,address2,city,state,zip,country,phone}).then((res)=>{
            if(res.data.status=="OK"){
                return Promise.resolve(res.data.msg)
            }
            else
            {
                this.$router.push("/mainPage/error")
                return Promise.reject(new Error(res.data.msg))
            }
        },(error)=>{
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        }).catch((error)=>{
            this.$router.push("/mainPage/error")
            return Promise.reject(error)
        })
    },
    signOut({commit}) {
        commit('setSignIn', false)
        commit('setAccount', null)
        localStorage.removeItem('account')
    },
    signIn({commit},{userName, password}){
        return this.$api.account.getAccount('getAccountByUsernameAndPassword', userName, password).then((res) => {
            if(res.data.status === 'OK' && res.data.msg === 'admin') {
                commit('setSignIn',true)
                commit('setAccount',res.data.data)
                localStorage.setItem('account', JSON.stringify(res.data.data))
                return Promise.resolve('OK')
            }
            else
            {
                return Promise.reject(new Error("请输入正确的管理员账号与密码!"))
            }
        },(error)=>{
            return Promise.reject(error)
        }).catch((error)=>{
            return Promise.reject(error)
        })
    }
}
